<?php

namespace app\api\trait;

use app\api\model\User;
use app\enum\Key;
use think\facade\Session;

trait LoginSession
{
    protected ?User $user = null;

    protected function getSessionId(): string
    {
        return Session::getId();
    }

    protected function getLoginUser(): User|null
    {
        $this->user = Session::get(Key::LOGIN_USER->value, null);
        return $this->user;
    }

    public function setLoginUser(User $user): void
    {
        Session::set(Key::LOGIN_USER->value, $user);
        $this->user = $user;
    }

    public function logoutUser(): void
    {
        Session::destroy();
    }

}
